package cn.kimming.wms.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import cn.kimming.wms.dao.ISystemMenuDAO;
import cn.kimming.wms.domain.Role;
import cn.kimming.wms.domain.SystemMenu;

@Repository("systemMenuDAO")
public class SystemMenuDAOImpl extends GenericDAOImpl<SystemMenu> implements ISystemMenuDAO{

	@Override
	public List<SystemMenu> listByParentId(Long parentId) {
		return queryForList("obj.parent.id = ?", parentId);
	}
	
	@Override
	public List<SystemMenu> listChildren() {
		return queryForList("obj.parent is not null");
	}

	@Override
	public SystemMenu getBySn(String sn) {
		return queryForObject("obj.sn = ?", sn);
	}

	@Override
	public List<SystemMenu> listByParentSn(String parentSn) {
		return queryForList("obj.parent.sn = ?", parentSn);
	}

	@Override
	public List<SystemMenu> listByParentSnAndRole(String parentSn, List<Role> roles) {
		Session session = sessionFactory.getCurrentSession();
		String hql = "SELECT m FROM Role r JOIN r.menus m WHERE m.parent.sn = :parentSn and r in (:roles)";
		Query query = session.createQuery(hql);
		query.setParameter("parentSn", parentSn);
		query.setParameterList("roles", roles);
		return query.list();
	}


	
}
